package com.example.zjsr.service.impl;

import com.example.zjsr.dao.UserDao;
import com.example.zjsr.dao.knowledge.KnowledgeMapper;
import com.example.zjsr.domain.Admin.knowledge.HeadLine;
import com.example.zjsr.domain.Admin.knowledge.Knowledge;
import com.example.zjsr.domain.Admin.knowledge.KnowledgeAnswer;
import com.example.zjsr.domain.Headline;
import com.example.zjsr.service.KnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lvyongwei
 */
@Service
public class KnowledgeServiceImpl implements KnowledgeService {

    @Autowired
    private KnowledgeMapper knowledgeMapper;

    @Autowired
    private UserDao userDao;

    /**
     * 添加知识互动内容
     * @param knowledge
     * @return
     */
    @Override
    public boolean addKnowledge(Knowledge knowledge) {
        return knowledgeMapper.addKnowledge(knowledge);
    }

    /**
     * 修改知识互动内容及答案
     * @param knowledge
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateKnowledge(Knowledge knowledge) {
        knowledgeMapper.updateKnowledge(knowledge);
        Integer knowledgeId = knowledge.getKnowledgeId();
        /**
         * 判断是否有修改答案
         */
        if(knowledge.getKnowledgeAnswers().size() != 0){
            knowledgeMapper.deleteKnowledgeAnswer(knowledgeId);
            knowledgeMapper.insertKnowledgeAnswer(knowledgeId,knowledge.getKnowledgeAnswers());
        }
    }

    /**
     * 查询所有知识互动内容
     * @param headLineId
     * @return
     */
    @Override
    public List<Knowledge> selectKnowledge(Integer headLineId) {
        return knowledgeMapper.selectKnowledge(headLineId);
    }

    /**
     * 根据知识互动id查询答案
     * @param knowledgeId
     * @return
     */
    @Override
    public List<KnowledgeAnswer> selectKnowledgeAnswer(Integer knowledgeId) {
        return knowledgeMapper.selectKnowledgeAnswer(knowledgeId);
    }

    /**
     * 一次性查询全部标题，知识互动及答案内容
     * @return
     */
    @Override
    public List<HeadLine> selectAllKnowledge() {
        /**
         * 查询所有标题
         */
        List<Headline> headline = userDao.getHeadline();
        List<HeadLine> headLines = headline.stream().map(headline1-> {
            /**
             * 赋值
             */
            HeadLine headLine = new HeadLine();
            headLine.setHeadlineId(headline1.getHeadlineId());
            headLine.setHeadlineTitle(headline1.getHeadlineTitle());
            List<Knowledge> knowledges = knowledgeMapper.selectKnowledge(headline1.getHeadlineId());
            if(knowledges.size() != 0){
                headLine.setKnowledges(knowledges);
            }else{
                headLine.setKnowledges(null);
            }
            return headLine;
        }).collect(Collectors.toList());
        return headLines;
    }
}
